package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.goods

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.example.library_common.album.view.grid_view.XFlowLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.glidmodule.GlideApp
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.GoodsDetailBean
import com.lxj.xpopup.core.AttachPopupView
import com.lxj.xpopup.core.BottomPopupView

class GoodSelectPop : BaseFragmentEx() {


    companion object {
        fun newInstance(): GoodSelectPop {
            val args = Bundle()
            val fragment = GoodSelectPop()
            fragment.arguments = args
            return fragment
        }
    }

//    init {
////        bgDrawableMargin=0
//    }

//    override fun getImplLayoutId(): Int {
//        return R.layout.good_select_pop_layout
//    }

    private lateinit var mColorGirdAdapter: CommAdapter<String>

    private var mColorSelectPos = 0

    private lateinit var mSizeGirdAdapter: CommAdapter<String>

    private var mSizeSelectPos = 0

    private var infoDao = GoodsDetailBean()

    private lateinit var goodsName: TextView

    private lateinit var infoDaoIcon: ImageView

    private lateinit var goodsPrice: TextView

    private lateinit var goodsStore: TextView

    private lateinit var selectedTv: TextView


    private var flag = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    fun setInfoDao(infoDao: GoodsDetailBean): GoodSelectPop {
        this.infoDao = infoDao
        fillData()
        return this
    }

    override fun getLayoutId(): Any {
        return R.layout.good_select_pop_layout
    }

//    override fun getPopupHeight(): Int {
//        return PixUtils.dp2px(300)
//    }
//
//    override fun getMaxHeight(): Int {
//        return PixUtils.dp2px(300)
//    }

//    override fun addInnerContent() {
//        val contentView =
//            LayoutInflater.from(context).inflate(implLayoutId, bottomPopupContainer, false)
//        bottomPopupContainer.addView(
//            contentView,
//            ViewGroup.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT, popupHeight
//            )
//        )
//    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        view.findViewById<View>(R.id.add).setOnClickListener {
            infoDao.goods.selectedCount += 1
            selectedTv.text = "${infoDao.goods.selectedCount}"
        }

        view.findViewById<View>(R.id.cut).setOnClickListener {
            if (infoDao.goods.selectedCount > 1) {
                infoDao.goods.selectedCount -= 1
            }
            selectedTv.text = "${infoDao.goods.selectedCount}"
        }

        selectedTv = view.findViewById(R.id.selectedTv)

        infoDaoIcon = view.findViewById(R.id.infoDaoIcon)


        goodsName = view.findViewById(R.id.goodsName)


        goodsPrice = view.findViewById(R.id.goodsPrice)


        goodsStore = view.findViewById(R.id.goodsStore)


        val mColorGird: XFlowLayout = view.findViewById(R.id.mColorGird)
        val mSizeGird: XFlowLayout = view.findViewById(R.id.mSizeGird)


        val goodsColorLs = infoDao.goods.goodsColorLs

        goodsColorLs.forEachIndexed { index, s ->
            if (s === infoDao.goods.selectedColorLabs) {
                mColorSelectPos = index
            }
        }

        mColorGirdAdapter =
            object : CommAdapter<String>(context, R.layout.good_select_pop_color_gird) {

                override fun convert(holder: XQuickViewHolder?, item: String?, position: Int) {
                    holder?.setText(R.id.mRTextView, item)
                        ?.setSelect(R.id.mRTextView, mColorSelectPos == position)
                        ?.setOnClickListener {

                            if (mColorSelectPos == position) return@setOnClickListener

                            mColorSelectPos = position

                            mColorGirdAdapter.notifyDataSetChanged()

                            infoDao.goods.addSelectedColorLab(item)
                        }
                }
            }
        mColorGird.adapter = mColorGirdAdapter

        val goodsSizeLs = infoDao.goods.goodsSizeLs
        goodsSizeLs.forEachIndexed { index, s ->
            if (s === infoDao.goods.selectedSizeLabs) {
                mSizeSelectPos = index
            }
        }

        mSizeGirdAdapter =
            object : CommAdapter<String>(context, R.layout.good_select_pop_color_gird) {

                override fun convert(holder: XQuickViewHolder?, item: String?, position: Int) {
                    holder?.setText(R.id.mRTextView, item)
                        ?.setSelect(R.id.mRTextView, mSizeSelectPos == position)
                        ?.setOnClickListener {

                            if (mSizeSelectPos == position) return@setOnClickListener

                            mSizeSelectPos = position
                            mSizeGirdAdapter.notifyDataSetChanged()
                            infoDao.goods.addSelectedSizeLab(item)
                        }
                }
            }
        mSizeGird.adapter = mSizeGirdAdapter

        flag = true

        fillData()
    }

    private fun fillData() {

        if (!flag) return
        mSizeGirdAdapter.clear()
        mSizeGirdAdapter.addAll(infoDao.goods.goodsSizeLs)

        mColorGirdAdapter.clear()
        mColorGirdAdapter.addAll(infoDao.goods.goodsColorLs)

        GlideApp.with(infoDaoIcon)
            .load(infoDao.goods.defaultImg)
            .centerCrop()
            .into(infoDaoIcon)

        goodsName.text = infoDao.goods.goodsName

        goodsPrice.text = "\$${infoDao.goods.goodsPrice}"

        goodsStore.text = "库存 ${infoDao.goods.goodsStock}件"

        selectedTv.text = "${infoDao.goods.selectedCount}"
    }


//    @SuppressLint("SetTextI18n")
//    override fun onCreate() {
//        super.onCreate()
//
//
//
//    }

}